package com.model.defaults;

import java.util.HashMap;
import java.util.Map;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.core.hibernate.ExceptionHandler;
import com.core.hibernate.exceptionevent.ExceptionEvent;
import com.core.managers.SessionFactoryManager;
import com.core.utils.GlobalConstants;
import com.model.UserContextName;
import com.model.defaults.actionbar.ModelDefaultActionbarItems;
import com.model.defaults.menu.ModelDefaultMenuItems;
import com.model.defaults.report.ModelDefaultReportColumns;
import com.model.defaults.tabsheet.ModelDefaultTabsheetItems;
import com.model.project.attributes.TaskComponent;
import com.model.project.attributes.TaskPriority;
import com.model.project.attributes.TaskResolution;
import com.model.project.attributes.TaskStatus;
import com.model.project.attributes.TaskType;

public class ModelDefaultItems {

	private static Map <String, UserContextName> userContexts = new HashMap <String, UserContextName>();



	private static UserContextName newUserContextName ( Session session,
	                                                    String name ) {

		UserContextName contextVisibleIn = null;
		if ( ModelDefaultItems.userContexts.get(name) == null ) {

			contextVisibleIn = new UserContextName();
			contextVisibleIn.setName(name);
			session.saveOrUpdate(contextVisibleIn);
			ModelDefaultItems.userContexts.put(contextVisibleIn.getName(), contextVisibleIn);

		}
		return contextVisibleIn;
	}



	private static void createProjectComponents ( Session session ) {

		Transaction transaction = null;

		try {
			transaction = session.beginTransaction();

			TaskComponent pojo = new TaskComponent();
			pojo.setName("Briefcase");
			session.save(pojo);

			pojo = new TaskComponent();
			pojo.setName("General");
			session.save(pojo);

			transaction.commit();
		} catch ( RuntimeException runtimeException ) {
			ExceptionHandler.handleException(runtimeException, new ExceptionEvent(ExceptionEvent.POJOSAMPLES));
			if ( transaction != null ) {
				if ( transaction.isActive() ) {
					try {
						transaction.rollback();
					} catch ( HibernateException hibernateException ) {
						ExceptionHandler.handleException(hibernateException, new ExceptionEvent(ExceptionEvent.POJOSAMPLES));
					}
				}
			}
		}

	}



	private static void createTaskStatuses ( Session session ) {

		Transaction transaction = null;

		try {
			transaction = session.beginTransaction();

			TaskStatus pojo = null;

			pojo = new TaskStatus();
			pojo.setName("Submitted");
			session.save(pojo);

			pojo = new TaskStatus();
			pojo.setName("Assigned");
			session.save(pojo);

			pojo = new TaskStatus();
			pojo.setName("Fixed");
			session.save(pojo);

			pojo = new TaskStatus();
			pojo.setName("Verified");
			session.save(pojo);

			pojo = new TaskStatus();
			pojo.setName("Reopened");
			session.save(pojo);

			pojo = new TaskStatus();
			pojo.setName("Declined");
			session.save(pojo);

			transaction.commit();
		} catch ( RuntimeException runtimeException ) {
			ExceptionHandler.handleException(runtimeException, new ExceptionEvent(ExceptionEvent.POJOSAMPLES));
			if ( transaction != null ) {
				if ( transaction.isActive() ) {
					try {
						transaction.rollback();
					} catch ( HibernateException hibernateException ) {
						ExceptionHandler.handleException(hibernateException, new ExceptionEvent(ExceptionEvent.POJOSAMPLES));
					}
				}
			}
		}

	}



	private static void createTaskTypeItems ( Session session ) {

		Transaction transaction = null;

		try {
			transaction = session.beginTransaction();

			TaskType pojo = null;

			pojo = new TaskType();
			pojo.setName("Cosmetic flow");
			session.save(pojo);

			pojo = new TaskType();
			pojo.setName("Data Corruption/Loss");
			session.save(pojo);

			pojo = new TaskType();
			pojo.setName("Documentation issue");
			session.save(pojo);

			pojo = new TaskType();
			pojo.setName("Incorrect operation");
			session.save(pojo);

			pojo = new TaskType();
			pojo.setName("Installation problem");
			session.save(pojo);

			pojo = new TaskType();
			pojo.setName("Localization issue");
			session.save(pojo);

			pojo = new TaskType();
			pojo.setName("Missing feature");
			session.save(pojo);

			pojo = new TaskType();
			pojo.setName("Slow performance");
			session.save(pojo);

			pojo = new TaskType();
			pojo.setName("System crash");
			session.save(pojo);

			pojo = new TaskType();
			pojo.setName("Unexpected behavior");
			session.save(pojo);
			
			pojo = new TaskType();
			pojo.setName("Unfriendly behavior");
			session.save(pojo);
			
			transaction.commit();
		} catch ( RuntimeException rte ) {
			ExceptionHandler.handleException(rte, new ExceptionEvent(ExceptionEvent.POJOSAMPLES));
			if ( transaction != null ) {
				if ( transaction.isActive() ) {
					try {
						transaction.rollback();
					} catch ( HibernateException hibernateException ) {
						ExceptionHandler.handleException(hibernateException, new ExceptionEvent(ExceptionEvent.POJOSAMPLES));
					}
				}
			}
		}

	}



	private static void createTaskPriorityItems ( Session session ) {

		Transaction transaction = null;

		try {
			transaction = session.beginTransaction();

			TaskPriority pojo = null;

			pojo = new TaskPriority();
			pojo.setName("Critical");
			session.save(pojo);

			pojo = new TaskPriority();
			pojo.setName("Major");
			session.save(pojo);

			pojo = new TaskPriority();
			pojo.setName("Medium");
			session.save(pojo);

			pojo = new TaskPriority();
			pojo.setName("Minor");
			session.save(pojo);


			transaction.commit();
		} catch ( RuntimeException rte ) {
			ExceptionHandler.handleException(rte, new ExceptionEvent(ExceptionEvent.POJOSAMPLES));
			if ( transaction != null ) {
				if ( transaction.isActive() ) {
					try {
						transaction.rollback();
					} catch ( HibernateException hibernateException ) {
						ExceptionHandler.handleException(hibernateException, new ExceptionEvent(ExceptionEvent.POJOSAMPLES));
					}
				}
			}
		}

	}



	private static void createTaskResolutionItems ( Session session ) {

		Transaction transaction = null;

		try {
			transaction = session.beginTransaction();

			TaskResolution pojo = null;

			pojo = new TaskResolution();
			pojo.setName("Always");
			session.save(pojo);

			pojo = new TaskResolution();
			pojo.setName("Sometimes");
			session.save(pojo);

			transaction.commit();
		} catch ( RuntimeException rte ) {
			ExceptionHandler.handleException(rte, new ExceptionEvent(ExceptionEvent.POJOSAMPLES));
			if ( transaction != null ) {
				if ( transaction.isActive() ) {
					try {
						transaction.rollback();
					} catch ( HibernateException hibernateException ) {
						ExceptionHandler.handleException(hibernateException, new ExceptionEvent(ExceptionEvent.POJOSAMPLES));
					}
				}
			}
		}

	}



	private static void createUserContextNames ( Session session ) {

		Transaction transaction = null;

		try {

			transaction = session.beginTransaction();

			ModelDefaultItems.newUserContextName(session, GlobalConstants.USERCONTEXT_ADMINISTRATION);
			ModelDefaultItems.newUserContextName(session, GlobalConstants.USERCONTEXT_COMPONENTLIST);
			ModelDefaultItems.newUserContextName(session, GlobalConstants.USERCONTEXT_DASHBOARD);
			ModelDefaultItems.newUserContextName(session, GlobalConstants.USERCONTEXT_GROUP);
			ModelDefaultItems.newUserContextName(session, GlobalConstants.USERCONTEXT_GROUPADD);
			ModelDefaultItems.newUserContextName(session, GlobalConstants.USERCONTEXT_GROUPDELETE);
			ModelDefaultItems.newUserContextName(session, GlobalConstants.USERCONTEXT_GROUPINFO);
			ModelDefaultItems.newUserContextName(session, GlobalConstants.USERCONTEXT_GROUPSLIST);
			ModelDefaultItems.newUserContextName(session, GlobalConstants.USERCONTEXT_GROUPUPDATE);
			ModelDefaultItems.newUserContextName(session, GlobalConstants.USERCONTEXT_PROJECT);
			ModelDefaultItems.newUserContextName(session, GlobalConstants.USERCONTEXT_PROJECTADD);
			ModelDefaultItems.newUserContextName(session, GlobalConstants.USERCONTEXT_PROJECTINFO);
			ModelDefaultItems.newUserContextName(session, GlobalConstants.USERCONTEXT_PROJECTSLIST);
			ModelDefaultItems.newUserContextName(session, GlobalConstants.USERCONTEXT_PROJECTUPDATE);
			ModelDefaultItems.newUserContextName(session, GlobalConstants.USERCONTEXT_PROJECTDELETE);
			ModelDefaultItems.newUserContextName(session, GlobalConstants.USERCONTEXT_REPORT);
			ModelDefaultItems.newUserContextName(session, GlobalConstants.USERCONTEXT_REPORTADD);
			ModelDefaultItems.newUserContextName(session, GlobalConstants.USERCONTEXT_REPORTINFO);
			ModelDefaultItems.newUserContextName(session, GlobalConstants.USERCONTEXT_REPORTSLIST);
			ModelDefaultItems.newUserContextName(session, GlobalConstants.USERCONTEXT_REPORTUPDATE);
			ModelDefaultItems.newUserContextName(session, GlobalConstants.USERCONTEXT_REPORTEXECUTE);
			ModelDefaultItems.newUserContextName(session, GlobalConstants.USERCONTEXT_ROLE);
			ModelDefaultItems.newUserContextName(session, GlobalConstants.USERCONTEXT_ROLEADD);
			ModelDefaultItems.newUserContextName(session, GlobalConstants.USERCONTEXT_ROLEDELETE);
			ModelDefaultItems.newUserContextName(session, GlobalConstants.USERCONTEXT_ROLEINFO);
			ModelDefaultItems.newUserContextName(session, GlobalConstants.USERCONTEXT_ROLEPERMISSIONS);
			ModelDefaultItems.newUserContextName(session, GlobalConstants.USERCONTEXT_ROLESLIST);
			ModelDefaultItems.newUserContextName(session, GlobalConstants.USERCONTEXT_ROLEUPDATE);
			ModelDefaultItems.newUserContextName(session, GlobalConstants.USERCONTEXT_SUBTASKADD);
			ModelDefaultItems.newUserContextName(session, GlobalConstants.USERCONTEXT_SUBTASKSLIST);
			ModelDefaultItems.newUserContextName(session, GlobalConstants.USERCONTEXT_TASK);
			ModelDefaultItems.newUserContextName(session, GlobalConstants.USERCONTEXT_TASKADD);
			ModelDefaultItems.newUserContextName(session, GlobalConstants.USERCONTEXT_TASKAINFO);
			ModelDefaultItems.newUserContextName(session, GlobalConstants.USERCONTEXT_TASKAUPDATE);
			ModelDefaultItems.newUserContextName(session, GlobalConstants.USERCONTEXT_TASKDELETE);
			ModelDefaultItems.newUserContextName(session, GlobalConstants.USERCONTEXT_TASKWORKLOG);
			ModelDefaultItems.newUserContextName(session, GlobalConstants.USERCONTEXT_TASKSLIST);
			ModelDefaultItems.newUserContextName(session, GlobalConstants.USERCONTEXT_USER);
			ModelDefaultItems.newUserContextName(session, GlobalConstants.USERCONTEXT_USERADD);
			ModelDefaultItems.newUserContextName(session, GlobalConstants.USERCONTEXT_USERINFO);
			ModelDefaultItems.newUserContextName(session, GlobalConstants.USERCONTEXT_USERSLIST);
			ModelDefaultItems.newUserContextName(session, GlobalConstants.USERCONTEXT_USERUPDATE);

			ModelDefaultItems.newUserContextName(session, GlobalConstants.USERCONTEXT_TEAMMEMBERLIST);
			ModelDefaultItems.newUserContextName(session, GlobalConstants.USERCONTEXT_TEAMMEMBERADD);
			ModelDefaultItems.newUserContextName(session, GlobalConstants.USERCONTEXT_TEAMMEMBERDELETE);
			ModelDefaultItems.newUserContextName(session, GlobalConstants.USERCONTEXT_TEAMMEMBERINFO);
			ModelDefaultItems.newUserContextName(session, GlobalConstants.USERCONTEXT_TEAMMEMBER);
			ModelDefaultItems.newUserContextName(session, GlobalConstants.USERCONTEXT_TEAMMEMBERUPDATE);

			ModelDefaultItems.newUserContextName(session, GlobalConstants.USERCONTEXT_VERSION);
			ModelDefaultItems.newUserContextName(session, GlobalConstants.USERCONTEXT_VERSIONADD);
			ModelDefaultItems.newUserContextName(session, GlobalConstants.USERCONTEXT_VERSIONDELETE);
			ModelDefaultItems.newUserContextName(session, GlobalConstants.USERCONTEXT_VERSIONINFO);
			ModelDefaultItems.newUserContextName(session, GlobalConstants.USERCONTEXT_VERSIONSLIST);
			ModelDefaultItems.newUserContextName(session, GlobalConstants.USERCONTEXT_VERSIONUPDATE);

			transaction.commit();

		} catch ( Exception e ) {

		}
	}



	public static void createDefaults ( SessionFactoryManager sessionFactoryManager ) {

		if ( sessionFactoryManager == null ) {
			return;
		}

		Session session = sessionFactoryManager.openSession();
		try {

			ModelDefaultItems.createTaskTypeItems(session);

			ModelDefaultItems.createUserContextNames(session);

			ModelDefaultItems.createProjectComponents(session);

			ModelDefaultItems.createTaskStatuses(session);

			ModelDefaultItems.createTaskPriorityItems(session);

			ModelDefaultItems.createTaskResolutionItems(session);

			ModelDefaultMenuItems.createMenuItems(session, ModelDefaultItems.userContexts);

			ModelDefaultTabsheetItems.createTabsheetItems(session, ModelDefaultItems.userContexts);

			ModelDefaultActionbarItems.createActionBarItems(session, ModelDefaultItems.userContexts);

			ModelDefaultSamples.createSamples(session, ModelDefaultItems.userContexts);

			ModelDefaultReportColumns.createReportColumns(session, ModelDefaultItems.userContexts);

		} finally {

			session.close();
		}
	}
}
